﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CourseArraging.Common; 
using System.Runtime.InteropServices;
using Stone.Library.Common;
using Stone.DataContracts;

namespace CourseArraging.Frm.xygl
{
    /// <summary>
    /// 版权所有
    /// 石世特@温州医学院 sst330381@gmail.com
    /// </summary> 
    public partial class FrmStuInfo : FrmToolbarBase
    {
        #region 字段属性

        //对象字段
        private ModeEntity modeEntity = new ModeEntity(); //默认true表示添加,false表示更新
        private static FrmStuInfo instance;
        protected YhxxService.YhxxServiceClient yhxxdao = new YhxxService.YhxxServiceClient();

        //事件字段
        public delegate void DelegateSearch();
        public event DelegateSearch SearchOK;

        //数据存储字段
        protected List<int> course_id_list=new List<int>();//课程文本框中的已选课程id
        protected List<string> course_name_list=new List<string>();//课程文本框中的已选课程name
        #endregion

        #region 构造器

        public static FrmStuInfo GetInstance()
        {
            if (instance == null || instance.IsDisposed)
            {
                instance = new FrmStuInfo();
                instance.MdiParent = GlobalVar.frmMain;
            }
            instance.BringToFront();
            return instance;
        }

        public FrmStuInfo()
        {
            InitializeComponent();
            initBaseInfo();
        }

        public FrmStuInfo(string id)
        {
            InitializeComponent();
            modeEntity.changeMode(id);
            AssignData(id);
            this.label_title.Text = "学员信息";
        }

        #endregion

        #region 事件

        //填写姓名
        private void txt_name_TextChanged(object sender, EventArgs e)
        {
            try
            {
                txt_spell.Text = String2pyHelper.GetPYStr(txt_name.Text.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "出现了未知的错误！");
                return;
            }
        }

        //添加课程信息
        private void pic_add_Click(object sender, EventArgs e)
        {
            string course_name = ucCombo_course1.comboBox1.Text;
            int course_id = int.Parse(ucCombo_course1.comboBox1.SelectedValue.ToString());

            if (!course_id_list.Contains(course_id))//是否已经存在选择的课程id
            {
                course_id_list.Add(course_id);
                course_name_list.Add(course_name);
                txt_course.Text += " " + ucCombo_course1.comboBox1.Text;
            }
        }

        //删除课程信息
        private void btn_refresh_Click(object sender, EventArgs e)
        {
            course_id_list.Clear();
            course_name_list.Clear();
            txt_course.Text = string.Empty;
        }

        #region 按钮动态效果
        private void pic_add_Mouse_down(object sender, MouseEventArgs e)
        {
            this.pic_add.Left += 1;
            this.pic_add.Top += 1;
        }
        private void pic_add_Mouse_up(object sender, MouseEventArgs e)
        {
            this.pic_add.Left -= 1;
            this.pic_add.Top -= 1;
        }
        #endregion

        #endregion

        #region 实现抽象方法

        public override void tsb_new_fun()
        {
            initBaseInfo();
        }

        public override void tsb_save_fun()
        {
            StuEntity stuentitiy = CollectData();
            try
            {
                if (modeEntity.formMode)
                    yhxxdao.addStuinfo(stuentitiy);
                else
                {
                    yhxxdao.updateStuinfo(stuentitiy);
                    SearchOK();
                }
                MessageBox.Show("保存成功！");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "保存失败");
            }
        }

        public override bool be4save()
        {
            if (txt_name.Text == string.Empty)
            {
                MessageBox.Show("请输入学员姓名 !");
                return false;
            }
            if (combo_sex.Text == string.Empty)
            {
                MessageBox.Show("请输入学员性别 !");
                return false;
            }
            if (txt_course.Text == string.Empty)
            {
                MessageBox.Show("请输入课程信息 !");
                return false;
            }
            if (txt_lxr.Text == string.Empty)
            {
                MessageBox.Show("请输入联系人信息 !");
                return false;
            }
            if (txt_gddh.Text == string.Empty&&txt_phone.Text == string.Empty)
            {
                MessageBox.Show("请输入联系方式 !");
                return false;
            }
            //if (txt_address.Text == string.Empty)
            //{
            //    MessageBox.Show("请输入联系地址 !");
            //    return false;
            //}
            //if (txt_bz.Text == string.Empty)
            //{
            //    MessageBox.Show("请输入备注信息!");
            //    return false;
            //}
            if (uCkcb1.course_time_string.Count == 0)
            {
                MessageBox.Show("请输入课程时间安排信息!");
                return false;
            }
            return true;
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化文本框
        /// </summary>
        public void initBaseInfo()
        {
            txt_course.Text = string.Empty;
            //txt_identify.Text = string.Empty;
            txt_name.Text = string.Empty;
            txt_phone.Text = string.Empty;
            txt_spell.Text = string.Empty;
            txt_course.Text = string.Empty;
            txt_lxr.Text = string.Empty;
            txt_gddh.Text = string.Empty;
            txt_address.Text = string.Empty;
            txt_bz.Text = string.Empty;
            course_id_list.Clear();
            course_name_list.Clear();
            this.uCkcb1.setCaption("选择教师可以授课的时间");
            this.uCkcb1.initeDataGrid();
        }

        /// <summary>
        /// 给实体赋值
        /// </summary>
        /// <returns>教师信息</returns>
        public StuEntity CollectData()
        {
            StuEntity stuEntity = new StuEntity();
            stuEntity.user_id = int.Parse(modeEntity.id);
            stuEntity.user_name = txt_name.Text;
            stuEntity.user_spell = txt_spell.Text;
            stuEntity.user_sex = combo_sex.Text;
            stuEntity.user_grade = ucCombo_grade1.comboBox1.Text;
            stuEntity.user_course = CommonHelper.string2string2DB(course_name_list);
            stuEntity.user_lxr = txt_lxr.Text;
            stuEntity.user_gddh = txt_gddh.Text.Trim();
            stuEntity.user_phone = txt_phone.Text;
            stuEntity.user_address = txt_address.Text;
            stuEntity.user_dep = ucCombo_dep1.comboBox1.SelectedValue.ToString();
            stuEntity.user_depname = ucCombo_dep1.comboBox1.Text;
            stuEntity.user_bz = txt_bz.Text;
            stuEntity.user_regedit = dateTimePicker1.Text;
            stuEntity.user_course_time = CommonHelper.string2string2DB(uCkcb1.course_time_string);
            stuEntity.user_arranged = "未安排";

            //seat是在安排的时候确定
            return stuEntity;
        }

        /// <summary>
        /// 给控件内容赋值
        /// </summary>
        /// <param name="stuEntity"></param>
        public void AssignData(string id)
        {
            StuEntity stuEntity = yhxxdao.GetStuinfoEntity(id);
            txt_name.Text=stuEntity.user_name;
            txt_spell.Text=stuEntity.user_spell;
            combo_sex.Text=stuEntity.user_sex;
            ucCombo_grade1.comboBox1.Text=stuEntity.user_grade ;
            txt_lxr.Text=stuEntity.user_lxr;
            txt_gddh.Text=stuEntity.user_gddh;
            txt_phone.Text=stuEntity.user_phone;
            txt_address.Text= stuEntity.user_address;
            ucCombo_dep1.comboBox1.Text=stuEntity.user_depname;
            txt_bz.Text=stuEntity.user_bz ;
            dateTimePicker1.Text=stuEntity.user_regedit;
            checkBox_arranged.Checked=stuEntity.user_arranged=="已安排" ? true:false;

            string[] coursename = CommonHelper.GetSplitString(stuEntity.user_course);
            string[] coursetime = CommonHelper.GetSplitString(stuEntity.user_course_time);
            for(int i=0;i<coursename.Length;i++)
            {
                course_name_list.Add(coursename[i]);
                txt_course.Text+=" "+coursename[i];
            }
            for (int i = 0; i < coursetime.Length; i++)
            {
                string[] tmpstring = coursetime[i].Split(new char[] { '-' });
                int tmp = 1;
                if (DateTime.Parse(tmpstring[1]) >= DateTime.Parse("18:00"))
                    tmp = 5;
                else if (DateTime.Parse(tmpstring[1]) >= DateTime.Parse("12:00"))
                    tmp = 3;
                uCkcb1.SetCellData(
                    int.Parse(tmpstring[0]), tmp, null, tmpstring[1] + "-" + tmpstring[2]);
            }
                
        }

        #endregion

    }
}
